package com.systemdevelopment.mainproject.View.SuperAdmin;

import com.jfoenix.controls.JFXButton;
import com.systemdevelopment.mainproject.Model.*;
import com.systemdevelopment.mainproject.View.oneInstanceController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableSet;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
        import javafx.scene.control.TableColumn;
        import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;

import java.util.ArrayList;

public class userTableController extends oneInstanceController {

    @FXML
    private JFXButton switchToBNormalButton;

    @FXML
    private TableColumn<NormalUser, String> password;

    @FXML
    private TableColumn<NormalUser, String> authority;

    @FXML
    private TableColumn<NormalUser, String> userName;

    @FXML
    private JFXButton showAllButton;

    @FXML
    private TableView<NormalUser> table;

    @FXML
    private JFXButton switchToAdminButton;


    private ObservableList<NormalUser> users;
    private ObservableList<NormalUser> tableItems;

    @FXML
    void showAll(ActionEvent event) {

    }

    @FXML
    void switchToAdmin(ActionEvent event) {

    NormalUser u= table.getSelectionModel().getSelectedItem();
        if(u.getClass().getSimpleName().equals(NormalUser.class.getSimpleName()))
        {
            UserFactory.ChangeNormalUserToAdmin(u);
            initTable();
        }
    }

    @FXML
    void switchToBNormal(ActionEvent event) {
        NormalUser u= table.getSelectionModel().getSelectedItem();
        if(u.getClass().getSimpleName().equals(Admin.class.getSimpleName()))
        {
            UserFactory.ChangeAdminToNormalUser((Admin) u);
            initTable();
        }

    }
    void initTable()
    {
        users= FXCollections.observableList(UserFactory.getAllUser());

        password.setCellValueFactory(new PropertyValueFactory("password"));
        password.setCellFactory(TextFieldTableCell.forTableColumn());

        authority.setCellValueFactory(new PropertyValueFactory("authority"));
        authority.setCellFactory(TextFieldTableCell.forTableColumn());

        userName.setCellValueFactory(new PropertyValueFactory("Account"));
        userName.setCellFactory(TextFieldTableCell.forTableColumn());

        table.setItems(users);
    }
}
